/**
 *	@project:	kybernetes
 *	@author:	D.Toepel
 *	@note:		---
 *	
 *	Copyright (C) 2009  D.Toepel <de.dtools> {at} <gmail> {dot} <com>
 *	
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *	
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 */
package dt.htwk.rmi;

import java.io.IOException;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import sun.security.provider.PolicyFile;

import dt.htwk.utilities.User;

/**
 * <h3>RMIServer</h3>
 *
 * @author DToepel
 * extends UnicastRemoteObject 
 */
public class RMIServer extends UnicastRemoteObject implements Serializable{//  implements RMIConnector, Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Registry registry;
	private boolean isRunning = false;
	private static String srvdir;
	private String addr = "localhost";

	protected static PolicyFile pf = new PolicyFile(
			RMIClient.class.getResource("server.policy"));
	
	/**
	 * @throws RemoteException
	 * 
	 * !!! wieder in protected aendern !!!
	 */
	public RMIServer() throws RemoteException {
		super();
		RMIServer.srvdir = System.getProperty("user.dir");
	}

	public void setSrvdir(String srvdir){
		RMIServer.srvdir = srvdir;
	}
	
	public static String getSrvdir(){
        System.out.println("request ...");
		return RMIServer.srvdir;
	}
	

	/**
	 * <h3>SrvStart</h3>
	 *
	 * @return
	 */
	public String SrvStart()
    {
    	String ausgabeTxT = "";
        try {
        	addr = InetAddress.getLocalHost().getHostAddress();
        	System.setProperty("java.rmi.server.hostname",addr);
        	registry = LocateRegistry.createRegistry(RMIConnector.RMI_PORT);

            // Interface implementieren ...
        	RMIConnector con = new RMISrvImpl();
            registry.rebind(RMIConnector.REGISTRY_NAME, con);
//        	Naming.rebind("//" + addr + ":" + RMIConnector.RMI_PORT + "/" + RMIConnector.REGISTRY_NAME, con);
            ausgabeTxT = "Der RMIServer(" + addr + ") wurde gestartet";
            isRunning = true;
        }catch (Exception e) {
        	ausgabeTxT = "Fehler:\nServer konnte nicht gestartet werden.";
        	ausgabeTxT = ausgabeTxT + "\n" + e;
        }
    	return ausgabeTxT;
    }
	
	public String SrvStop()
    {
    	String ausgabeTxT = "";
        try {
            registry.unbind(RMIConnector.REGISTRY_NAME);
            if (registry != null)
                UnicastRemoteObject.unexportObject(registry, true);
            registry = null;
            ausgabeTxT = "Der RMIServer wurde angehalten";
            isRunning = false;
        }catch (Exception e) {
        	ausgabeTxT = "Fehler:\nServer konnte nicht angehalten werden.";
        	ausgabeTxT = ausgabeTxT + "\n" + e;
        }
        return ausgabeTxT;
    }

	public boolean isRunning(){
		return this.isRunning;
	}
}
